/**
 * Created by Administrator on 2017/10/5 0005.
 */
(function($j){
    function init(){
        renderData();
        bindBtnEvent();
    }

    function renderData(){
        $j.restPost('/user/doGetUserInfo').done(function(mess,data){
            if(data.nickName != undefined){
                $('.nickName').val(data.nickName);
                $('.sex').val(data.sex);
                $('.phone').val(data.phone);
                $('.addr').val(data.addr);
            }
        }).fail(function(mess){
           $.toast(mess);
        });

    }

    function bindBtnEvent(){

        //点击保存按钮
        $('.save').off('click').on('click',function(){
            $('.msgInfo').html('');
            var nickName = $('.nickName').val();
            var sex = $('.sex').val();
            var phone = $('.phone').val();
            var addr = $('.addr').val();

            if(nickName == ''){
                $('.msgInfo').html('<span class="iconfont icon-cuowu" style="margin-right: 5px;"></span>真实姓名不能为空');
                return false;
            }
            if(phone == ''){
                $('.msgInfo').html('<span class="iconfont icon-cuowu" style="margin-right: 5px;"></span>手机号码不能为空');
                return false;
            }
            if(addr == ''){
                $('.msgInfo').html('<span class="iconfont icon-cuowu" style="margin-right: 5px;"></span>家庭住址不能为空');
                return false;
            }

            if(!$j.IsTel(phone)){
                $('.msgInfo').html('<span class="iconfont icon-cuowu" style="margin-right: 5px;"></span>手机号码格式有误');
                return false;
            }

            var param = {
                nickName:nickName,
                sex:sex,
                phone:phone,
                addr:addr
            }

            $j.restPost('/user/doUpdate',param).done(function(mess,data){
                $.modal({
                    title: '提示',
                    text: '资料完善成功',
                    buttons: [
                        {
                            text: '返回上一页面',
                            onClick: function () {
                                $.router.back();
                            }
                        }
                    ]
                });
            }).fail(function(mess){
                $.toast(mess);
            })
        });
    }

    init();
})(jQuery);